package com.red.wood.module.ircs.mybatis.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.red.wood.common.domain.Page;
import com.red.wood.common.springjdbc.SqlServerDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.ircs.mybatis.dao.IrcsDomainInfoLinkDao;

@Service
public class IrcsDomainInfoLinkService {

	@Autowired
	private IrcsDomainInfoLinkDao dao;
	
	@Autowired
	private SqlServerDao sqlServerDao;
	
	public Map<String, Object> getPageData(IMap param) {
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		IMap map = sqlServerDao.getUserLevelInfo(param.getString("userId"));
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(map.get("loginUser")));
		String provinceId = CommonUtils.getProvinceId(userProvince);
		int pageSize = page.getPageSize();
		param.put("userLevel", map.get("userLevel"));
		param.put("userProvince", userProvince);
		param.put("provinceId", provinceId);
		param.put("pageSize", pageSize);
		param.put("offset", offset);
		
		long count = 0;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = dao.getCount(param);
		list = getList(dao.getPageData(param));
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String,Object>> getList(List<Map<String,Object>> list){
		if(list != null && list.size() > 0) {
			for(Map<String,Object> map : list) {
				String setMode = CommonUtils.valueOf(map.get("setMode"));
				String regType = CommonUtils.valueOf(map.get("regType"));
				if(StringUtils.equals(setMode, "0")) {
					setMode = "专线";
				}else if(StringUtils.equals(setMode, "1")) {
					setMode = "虚拟主机";
				}else if(StringUtils.equals(setMode, "2")) {
					setMode = "虚拟存储";
				}else if(StringUtils.equals(setMode, "3")) {
					setMode = "应用托管容器";
				}else if(StringUtils.equals(setMode, "999")) {
					setMode = "其他";
				}
				if(StringUtils.equals(regType, "0")) {
					regType = "无";
				}else if(StringUtils.equals(regType, "1")) {
					regType = "经营性网站";
				}else if(StringUtils.equals(regType, "2")) {
					regType = "非经营性网站";
				}else if(StringUtils.equals(regType, "3")) {
					regType = "SP";
				}else if(StringUtils.equals(regType, "4")) {
					regType = "BBS";
				}else if(StringUtils.equals(regType, "999")) {
					regType = "其他";
				}
				map.put("setMode", setMode);
				map.put("regType", regType);
			}
		}
		return list;
	}
}